<! Controller -->
<?php
	require_once ('../../models/ProductCategory.php');
	
	if(isset($_POST['id']))
	{
		$row = $_POST;
		$response = ProductCategory::Validate($row);
		if($response === true)
			$response=ProductCategory::Update($row);
		if($response === true)
        	header("Location: index.php");
		
	}
	else 
	{
		$row = ProductCategory::Get($_REQUEST['id']);
	}
	
?>

<! View -->
<!DOCTYPE html>
<html lang="en">
	<?php include('../../inc/head.php'); ?>
	<body>
		<div>
			<?php include('../../inc/nav.php') ?>
		</div>
		<div id="content">
			
			<? if(isset($response)): ?>
            	<dl class="dl-horizontal error">
                <? foreach ($response as $key => $value) { ?>
                	<dt><?=$key?></dt>
                    <dd><?=$value?></dd>
                <? } ?>                                         
                </dl>
            <? endif; ?>
			
			<form class="form-horizontal" action="" method="post">
				<input type="hidden" name="id" value="<?=$row['id']?>" />
				
				<? function RenderInput($propertyName, $inputtype){ ?>
					<? global $row ?>
					<? global $response ?>
					<div class="control-group">
						 <label class="control-label" for="<?=$propertyName?>"><?=$propertyName?>:</label>
                         <div class="controls">
                         	<input  type="<?=$inputtype?>" name="<?=$propertyName?>" id="<?=$propertyName?>" value="<?=$row[$propertyName]?>"
                            	   class="<?=isset($response[$propertyName]) ? 'error' : '' ?>"
                         	 />
                         	<? if(isset($response[$propertyName])): ?>
                          		<span class="error"><?=$response[$propertyName]?></span>
                         	<? endif; ?>
                         </div>

					</div>
				<? } ?>
				
				<?
				RenderInput('created_at', 'datetime');
				RenderInput('updated_at', 'datetime');
				RenderInput('Name', 'text');
				?>
				
				<div class="control-group">
					<div class="controls">
						<input type="submit" value="Submit" class="btn btn-primary">
					</div>
				</div>
			</form>
		</div>
			<?php include('../../inc/footer.php'); ?>
		</div>
		<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js"></script>
        <script type="text/javascript">
        	$(function(){
            
            	$("form").validate(
                {
                	rules: { created_at: { required: true} }
                }
	            );

				
				$("form.error").slideUp('slow').slideDown('slow');
				$("input[type='datetime']").datepicker();
			});
		</script>
	</body>
</html>